<template>
  <div v-show="closeFlag">
    <div class="k-resume-modal">
      <span class="ksd-resume-close" @click="handleClose"><i class="iconfont icon-shanchu4"></i></span>
      <div class="flex">
        <div class="bannerbox">
          <img src="/imgs/template2.jpg" alt="banner">
          <ul class="slick-dots slick-dots-bottom custom-slick-item" style="display: block;">
            <li class="slick-active">
              <div class="guide-team-banner__slick-item"><span
                  class="guide-team-banner__slick-item-title">精心规划</span><span

                  class="guide-team-banner__slick-item-desc">在简历服务中，你只需要耐心等待简历的生成</span>
              </div>
            </li>
            <li class="">
              <div class="guide-team-banner__slick-item"><span
                  class="guide-team-banner__slick-item-title">会员服务</span><span

                  class="guide-team-banner__slick-item-desc">升级会员免费获取简历服务、升级会员获取面试题题免费学习，设计模式免费学习</span>
              </div>
            </li>
            <li class="">
              <div class="guide-team-banner__slick-item"><span
                  class="guide-team-banner__slick-item-title">简历就业服务</span><span

                  class="guide-team-banner__slick-item-desc">赠送三个月平台VIP权限，畅享面试就业、设计模式相关课程</span>
              </div>
            </li>
          </ul>
        </div>
        <div class="ksd-base-info">
          <div class="input-items"><select v-model="form.isJob" class="gda-input gda-input-lg gda-input-empty">
            <option value="1">已就业</option>
            <option value="0">待就业</option>
          </select>
          </div>
          <div class="input-items"><input placeholder="请填写简历标题（比如：xxx-个人简历）" id="title" v-model="form.title" type="text"
                                          class="gda-input gda-input-lg gda-input-empty" maxlength="40"></div>
          <div class="input-items"><input placeholder="请填写联系方式（电话）"  id="telephone" v-model="form.telephone" type="text"
                                          class="gda-input gda-input-lg gda-input-empty"  maxlength="11"></div>
          <div class="input-items"><input placeholder="请填写微信" type="text" id="weixincode" v-model="form.weixincode"
                                          class="gda-input gda-input-lg gda-input-empty"  maxlength="20">
          </div>
          <div class="input-items">
            <textarea class="gda-input" v-model="form.description" id="description" style="height: 218px;"
                      placeholder="填写你的擅长的技术、期望。没有可以写【暂无】"
                      maxlength="600"></textarea>
          </div>
          <div class="mt20">
            <button type="button" :disabled="sureDisable" @click.prevent.stop="handleSubmit" class="gda-btn"
                    style="height: 40px;border-radius: 6px;background:#eeaa80;color:#fff;"><span>开始制作</span></button>
          </div>
        </div>
      </div>
    </div>
    <div class="k-resume-diaolog"></div>
    <teleport to="body">
      <ksd-resume-sure ref="resumeSureRef" @weixin="handleCopyWeixin" @pay="handleOpenPayDiaolog"></ksd-resume-sure>
      <ksd-resume-pay ref="resumePayRef"  @close="handleLoadRefresh"></ksd-resume-pay>
    </teleport>
  </div>
</template>
<script setup>
const closeFlag = ref(false)
const sureDisable = ref(false)
const userStore = useStore.userState()
const resumeSureRef = ref({})
const resumePayRef = ref({})
const currentObj = ref({})
const form = ref({
  id: "",
  title: "",
  description: "",
  resumeFile: "",
  resumeNewFile: "",
  isJob: 0,
  weixincode: "",
  telephone: "",
  rType: 4,
})
const handleLoadRefresh = () => {
  window.location.reload()
}
const handleOpen = (item) => {
  closeFlag.value = true
  if (item && item.id){
    form.value = item
  }else{
    form.value.title = "《"+userStore.username+"》个人简历"
  }
}
const handleClose = () => {
  closeFlag.value = false
  form.value = {
    id: "",
    title: "",
    description: "",
    weixincode: "",
    isJob: 0,
    telephone: "",
    rType: 4,
  }
}

const handleOpenPayDiaolog = async () => {
  await useLoginDialog()
  resumePayRef.value.handleOpen(currentObj.value.id, currentObj.value.price)
  resumeSureRef.value.handleClose()
}

const handleCopyWeixin = async () => {
  resumeSureRef.value.handleClose()
  window.location.reload()
}

function isValidPhoneNumber(phoneNumber) {
  var regex = /^1[3-9]\d{9}$/;
  return regex.test(phoneNumber);
}

const handleSubmit = debounce(async () => {
  try {
    await useLoginDialog()

    if (!form.value.title) {
      useMessage().error("请输入标题")
      getDom("title").focus()
      return;
    }

    if (!form.value.telephone) {
      useMessage().error("请输入联系方式");
      getDom("telephone").focus()
      return;
    }

    if(!isValidPhoneNumber(form.value.telephone)){
      useMessage().error("手机不合法");
      getDom("telephone").focus()
      return;
    }

    if (!form.value.weixincode) {
      useMessage().error("请输入微信");
      getDom("weixincode").focus()
      return;
    }

    if (!form.value.description) {
      useMessage().error("请输入描述");
      getDom("description").focus()
      return;
    }

    sureDisable.value = true
    const resp = await useResumeSaveUpdateApi(form.value)
    useMessage().success("操作成功")
    currentObj.value.id = resp.data.id
    currentObj.value.price = resp.data.price
    resumeSureRef.value.handleOpen()
    handleClose()
    sureDisable.value = false
  } catch (e) {
    sureDisable.value = false
    useMessage().error(e.message)
  }
},1000)

defineExpose({
  handleOpen
})
</script>
<style lang="scss" scoped>
.k-resume-diaolog {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 199;
  overflow: auto;
  background: rgba(0, 0, 0, 0.5);
}

.ksd-resume-close {
  position: absolute;
  right: 10px;
  top: 5px;
  z-index: 1000;
  cursor: pointer;
}

.k-resume-modal {
  display: inline-block;
  text-align: left;
  vertical-align: middle;
  box-sizing: border-box;
  color: #333;
  font-variant: tabular-nums;
  line-height: 1.5;
  list-style: none;
  font-feature-settings: "tnum";
  border-radius: 8px;
  width: 840px;
  background: #fff;
  position: fixed;
  margin: 0 auto;
  padding: 30px;
  z-index: 200;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  .bannerbox {
    width: 50%;
  }
}

.custom-slick-item {
  position: relative;
  bottom: 6px;
  padding: 0 32px 0;
  background: #f0f6ff;
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  border-bottom-left-radius: 12px;
}


.guide-team-banner__slick-item {
  display: flex;
  flex: 1 0 0;
  flex-direction: column;
  gap: 4px;
  align-items: flex-start;
  justify-content: center;
  padding: 4px 12px;
  cursor: pointer
}

.guide-team-banner__slick-item:hover .guide-team-banner__slick-item-title, .guide-team-banner__slick-item:hover .guide-team-banner__slick-item-desc {
  color: #2254f4
}

.guide-team-banner__slick-item-title {
  font-weight: bold;
}

.guide-team-banner__slick-item-desc {
  color: #7f8792;
  opacity: .6
}

.ksd-base-info {
  padding: 0 0 0 30px;
  width: 50%;

  button {
    width: 100%;
  }

  .input-items {
    margin-bottom: 15px;
  }
}

.gda-input {
  box-sizing: border-box;
  font-variant: tabular-nums;
  list-style: none;
  font-feature-settings: "tnum";
  position: relative;
  display: inline-block;
  width: 100%;
  height: 60px;
  padding: 4px 16px;
  color: #333;
  font-size: 14px;
  background-image: none;
  border: 2px solid #eff5fe;
  border-radius: 6px;
  transition: all .3s, padding-right 0s;
}
</style>